﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TournamentsForm.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Defines the TournamentsForm type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MtgMC
{
    #region Namespace Imports

    using System;
    using System.Globalization;
    using System.Linq;
    using System.Windows.Forms;

    using DataModel;

    #endregion


    /// <summary>
    /// Describes and manipulates the tournaments form.
    /// </summary>
    public partial class TournamentsForm : Form
    {
        #region Constants and Fields

        private readonly ApplicationData _applicationData;

        #endregion


        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TournamentsForm"/> class.
        /// </summary>
        /// <param name="applicationData">
        /// The application data.
        /// </param>
        public TournamentsForm(ApplicationData applicationData)
        {
            _applicationData = applicationData;
            InitializeComponent();

            _tournamentsListBox.DisplayMember = "DisplayName";

            UpdateListBox();
            UpdateControls();
        }

        #endregion


        #region Properties

        private Tournament SelectedTournament
        {
            get
            {
                return _tournamentsListBox.SelectedValue as Tournament;
            }
        }

        #endregion


        #region Methods

        private void AddMenuItemClick(object sender, EventArgs e)
        {
            ShowEditTournamentDialog(null);
        }


        private void CloseMenuItemClick(object sender, EventArgs e)
        {
            Close();
        }


        private void EditMenuItemClick(object sender, EventArgs e)
        {
            ShowEditTournamentDialog(SelectedTournament);
        }


        private void RemoveMenuItemClick(object sender, EventArgs e)
        {
            string text = string.Format(
                CultureInfo.CurrentCulture, "Are you sure you want to delete {0} tournament?", SelectedTournament.Name);

            DialogResult dialogResult = MessageBox.Show(
                text, 
                "Confirm Delete", 
                MessageBoxButtons.YesNo, 
                MessageBoxIcon.Question, 
                MessageBoxDefaultButton.Button2);

            if (dialogResult != DialogResult.Yes)
            {
                return;
            }

            _applicationData.Tournaments.Remove(SelectedTournament);
            UpdateListBox();
        }


        private void ShowEditTournamentDialog(Tournament tournament)
        {
            var form = new EditTournamentForm(_applicationData, tournament);
            DialogResult dialogResult = form.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                UpdateListBox();
            }

            form.Dispose();
        }


        private void TournamentsListBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateControls();
        }


        private void UpdateControls()
        {
            bool isItemSelected = SelectedTournament != null;

            _editMenuItem.Enabled = isItemSelected;
            _removeMenuItem.Enabled = isItemSelected;
        }


        private void UpdateListBox()
        {
            _tournamentsListBox.DataSource = _applicationData.Tournaments.ToList();
            _tournamentsListBox.Update();
        }

        #endregion
    }
}